package com.wut.clock.exception;

import com.wut.clock.common.api.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author ZYH
 * @unit D9Lab
 * @description shiro权限控制和登录认证异常处理
 * @date 2021/12/14 20:06
 */
@RestControllerAdvice
@Slf4j
public class ShiroException {

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthorizedException.class)
    public CommonResult authorizationException(UnauthorizedException e){
        log.error("没有权限：----------------{}",e.getMessage());
        return CommonResult.failed("没有操作权限："+e.getMessage());
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(UnauthenticatedException.class)
    public CommonResult authenticationException(UnauthenticatedException e){
        log.error("没有登录认证：----------------{}",e.getMessage());
        return CommonResult.failed("请先进行登录认证："+e.getMessage());
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public CommonResult handleIncorrectCredentialsException(IncorrectCredentialsException e){
        log.error("密码错误：---------------{}",e.getMessage());
        return CommonResult.failed("用户名或密码错误"+e.getMessage());
    }
}
